﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using NCKH.models;
using NCKH;
namespace NCKH.Test
{
    public class SpaceConstraints
    {
        public static void ConstraintBasicCompulsorySpace(XmlDocument xmlDoc, XmlNode root, string pathFile)
        {
            XmlNode nodeParent = xmlDoc.CreateNode(XmlNodeType.Element, "ConstraintBasicCompulsorySpace", null);
            {
                //-------------------------------------------------------
                XmlNode nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Weight_Percentage", null);
                nodeChild.InnerText = "100";
                nodeParent.AppendChild(nodeChild);
                //-------------------------------------------------------
                nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Active", null);
                nodeChild.InnerText = "true";
                nodeParent.AppendChild(nodeChild);
                //-------------------------------------------------------
                nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Comments", null);
                nodeParent.AppendChild(nodeChild);
            }
            root.AppendChild(nodeParent);
            xmlDoc.Save(pathFile);
        }
        public static void ConstraintActivityTagPreferredRooms(XmlDocument xmlDoc, XmlNode root, string pathFile, List<V_PHONGHOC> phongHocs)
        {
            Context db = new Context();

            var activityTags = db.EXM_HinhThucThis.Distinct();

            foreach (var activityTag in activityTags)
            {
                XmlNode nodeParent = xmlDoc.CreateNode(XmlNodeType.Element, "ConstraintActivityTagPreferredRooms", null);
                {
                    XmlNode nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Weight_Percentage", null);
                    nodeChild.InnerText = "100";
                    nodeParent.AppendChild(nodeChild);
                    //------------------------------------------------------------------------------------
                    nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Activity_Tag", null);
                    nodeChild.InnerText = activityTag.ID_nhan.ToString();
                    nodeParent.AppendChild(nodeChild);
                    //------------------------------------------------------------------------------------
                    var phongs = phongHocs.Where(w => w.Thuc_hanh == (activityTag.ID_nhan == 0 ? false : true)).ToList();
                    if (phongs.Count == 0 || phongs == null) continue;

                    if (phongs.Count == 1)
                    {
                        ConstraintActivityTagPreferredRoom(xmlDoc, root, pathFile, activityTag, phongs.Single());
                        continue;
                    }
                    //------------------------------------------------------------------------------------
                    nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Number_of_Preferred_Rooms", null);
                    nodeChild.InnerText = phongs.Count.ToString();
                    nodeParent.AppendChild(nodeChild);

                    foreach (var idPhong in phongs)
                    {
                        nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Preferred_Room", null);
                        nodeChild.InnerText = idPhong.ID_phong.ToString();
                        nodeParent.AppendChild(nodeChild);
                    }
                    //-------------------------------------------------------
                    nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Active", null);
                    nodeChild.InnerText = "true";
                    nodeParent.AppendChild(nodeChild);
                    //-------------------------------------------------------
                    nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Comments", null);
                    nodeParent.AppendChild(nodeChild);
                }
                root.AppendChild(nodeParent);
                xmlDoc.Save(pathFile);
            }
        }
        public static void ConstraintActivityTagPreferredRoom(XmlDocument xmlDoc, XmlNode root, string pathFile, EXM_HinhThucThi activityTag, V_PHONGHOC phongHoc)
        {
            XmlNode nodeParent = xmlDoc.CreateNode(XmlNodeType.Element, "ConstraintActivityTagPreferredRoom", null);
            {
                XmlNode nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Weight_Percentage", null);
                nodeChild.InnerText = "100";
                nodeParent.AppendChild(nodeChild);
                //------------------------------------------------------------------------------------
                nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Activity_Tag", null);
                nodeChild.InnerText = activityTag.ID_nhan.ToString();
                nodeParent.AppendChild(nodeChild);
                //------------------------------------------------------------------------------------
                nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Room", null);
                nodeChild.InnerText = phongHoc.ID_phong.ToString();
                nodeParent.AppendChild(nodeChild);
                //-------------------------------------------------------
                nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Active", null);
                nodeChild.InnerText = "true";
                nodeParent.AppendChild(nodeChild);
                //-------------------------------------------------------
                nodeChild = xmlDoc.CreateNode(XmlNodeType.Element, "Comments", null);
                nodeParent.AppendChild(nodeChild);
            }
            root.AppendChild(nodeParent);
            xmlDoc.Save(pathFile);
        }
    }
}